package template.page.admin;

import java.util.List;

import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.control.AbstractLink;
import org.apache.click.control.ActionButton;
import org.apache.click.control.ActionLink;
import org.apache.click.control.Column;
import org.apache.click.control.PageLink;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.LinkDecorator;

import template.page.BorderPage;

@SuppressWarnings("serial")
public class ManageScores extends BorderPage {

    public String title = "Manage Scores";

	private Table scoreTable = new Table("scoreTable");
	private PageLink editScoreLink = new PageLink("Edit",EditScore.class);
	private ActionButton createScoreButton = new ActionButton("addScoreButton","Add Score");
	private ActionLink deleteScoreLink = new ActionLink("Delete",this,"onDeleteScoreClick");	
	
	@SuppressWarnings("rawtypes")
	public ManageScores() 
	{
		addControl(scoreTable);
		addControl(editScoreLink);
		addControl(deleteScoreLink);
		addControl(createScoreButton);
		
		// Setup table
		scoreTable.setClass(Table.CLASS_ITS);

		scoreTable.addColumn(new Column("id"));
		scoreTable.addColumn(new Column("playerName"));
		scoreTable.addColumn(new Column("gameName"));
		scoreTable.addColumn(new Column("points"));
		scoreTable.setSortedColumn("playerName");
        
		editScoreLink.setImageSrc("/assets/images/actionEdit.gif");
		editScoreLink.setTitle("Edit game");
        
		createScoreButton.setTitle("Create score");
		createScoreButton.setListener(this, "onCreateScoreClick");
        
		deleteScoreLink.setImageSrc("/assets/images/actionDelete.gif");
		deleteScoreLink.setTitle("Delete score");
		deleteScoreLink.setAttribute("onclick",
            "return window.confirm('Are you sure you want to delete this record?');");

        Column column = new Column("Action");
        column.setTextAlign("center");
        AbstractLink[] links = new AbstractLink[] { editScoreLink, deleteScoreLink };
        column.setDecorator(new LinkDecorator(scoreTable, links, "id"));
        column.setSortable(false);
        scoreTable.addColumn(column);

        // Table rowList will be populated through a DataProvider which loads
        // data on demand.
        scoreTable.setDataProvider(new DataProvider() {

            public List getData() {
                return getGameSessionService().getAllScores();
            }
        });

        // Below we setup the table to preserve it's state (sorting and paging)
        // while editing customers
        scoreTable.getControlLink().setActionListener(new ActionListener() {

            public boolean onAction(Control source) {
                // Save Table sort and paging state between requests.
                // NOTE: we set the listener on the table's Link control which is invoked
                // when the Link is clicked, such as when paging or sorting.
                // This ensures the table state is only saved when the state changes, and
                // cuts down on unnecessary session replication in a cluster environment.
            	scoreTable.saveState(getContext()); 
                return true;
            }
        });

        // Restore the table sort and paging state from the session between requests
        scoreTable.restoreState(getContext()); 
	}
	
	/**
     * Handle the delete row click event.
     */    
    public boolean onDeleteScoreClick() {
        Long id = deleteScoreLink.getValueLong();
        getGameSessionService().deleteScore(id);
        return true;
    }
    
	/**
     * Handle the delete row click event.
     */    
    public boolean onCreateScoreClick() {
    	setRedirect(EditScore.class);        
        return true;
    }

}
